package in.muton.edushare.pages.publicpages.userpages;

import in.muton.edushare.pages.publicpages.userpages.ProfileTopMenu.MenuItems;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.PageParameters;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.protocol.http.RequestUtils;

import com.edushare.common.dao.ProfessionalDetailsDAO;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.ProfessionalDetails;

public class UserProfile extends UserProfileBase {

	public UserProfile(PageParameters params) {
		super(params);
		loadPanel();
	}

	private void loadPanel() {
		
		ListView<ProfessionalDetails> currList = 
			new ListView<ProfessionalDetails>
			("currList", new PropertyModel<List<ProfessionalDetails>>(UserProfile.this,"currPost")){

				private static final long serialVersionUID = 2825211474986054897L;

				@Override
				protected void populateItem(ListItem<ProfessionalDetails> item) {
					item.add(new CurrFrag("currSpan", UserProfile.this, item.getModel()));
				}
			
		};
		
		add(currList);
		
		ListView<ProfessionalDetails> pastExp =
			new ListView<ProfessionalDetails>("pastExp", new PropertyModel<List<ProfessionalDetails>>(UserProfile.this,"pastExp")){

				private static final long serialVersionUID = -8972423021895157954L;

				@Override
				protected void populateItem(ListItem<ProfessionalDetails> item) {
					item.add(new Label("posTtl", item.getModelObject().getPositionTitle()));
					item.add(new Label("orgName", item.getModelObject().getOrgName()));
				}
			
		};
		add(pastExp);
		
		ListView<ProfessionalDetails> pastEdu =
			new ListView<ProfessionalDetails>("pastEdu", new PropertyModel<List<ProfessionalDetails>>(UserProfile.this,"pastEdu")){


				private static final long serialVersionUID = 5343551564144039833L;

				@Override
				protected void populateItem(ListItem<ProfessionalDetails> item) {
					item.add(new Label("eduTtl", item.getModelObject().getPositionTitle()));
					item.add(new Label("schoolName", item.getModelObject().getOrgName()));
				}
			
		};
		add(pastEdu);
		add(new MultiLineLabel("skills", new PropertyModel<String>(super.userModel,"expertise")));
		
		PageParameters pp = new PageParameters();
		pp.add("0", super.userModel.getObject().getUserName());
		BookmarkablePageLink<Void> userPageLink = new BookmarkablePageLink<Void>("profileLink",UserProfile.class,pp);
		add(userPageLink);
		userPageLink.add(new Label("url",RequestUtils.toAbsolutePath(RequestCycle.get().urlFor(UserProfile.class,pp).toString())));
			
		
		add(new Label("summary", new PropertyModel<String>(super.userModel, "aboutMe")).setEscapeModelStrings(false));
		Label intrst = 
			new Label("interests", new PropertyModel<String>(super.userModel, "interests")){
			
			private static final long serialVersionUID = -2339034274158734614L;

			@Override
			public boolean isVisible() {
				if(getDefaultModelObject()==null || !StringUtil.isValid(getDefaultModelObjectAsString())){
					return false;
				}
				return true;
			}
		};
		intrst.setEscapeModelStrings(false);
		add(intrst);
		
		Label hnr = new Label("honors", new PropertyModel<String>(super.userModel, "honors")){
			
			private static final long serialVersionUID = -2846799402265472002L;

			@Override
			public boolean isVisible() {
				return getDefaultModel()!=null && StringUtil.isValid(getDefaultModelObjectAsString());
			}
		};
		hnr.setEscapeModelStrings(false);
		add(hnr);
		
		add(new Label("num", new PropertyModel<String>(super.userModel, "phoneNo")));
		add(new Label("mail", new PropertyModel<String>(super.userModel, "email")));
		
	}
	
	@Override
	public MenuItems getSelectedMenu() {
		return MenuItems.SUMMARY;
	}
	
	public List<ProfessionalDetails> getCurrPost(){
		return new ProfessionalDetailsDAO(getSessionORMInterface()).getCurrentPositionsAndDegrees(super.userModel.getObject());
	}
	
	public List<ProfessionalDetails> getPastExp(){
		List<ProfessionalDetails> proList = new ProfessionalDetailsDAO(getSessionORMInterface()).getPositions(super.userModel.getObject());
		return filterPastProDetails(proList);
	}
	
	public List<ProfessionalDetails> getPastEdu(){
		List<ProfessionalDetails> proList = new ProfessionalDetailsDAO(getSessionORMInterface()).getdegrees(super.userModel.getObject());
		return filterPastProDetails(proList);
	}
	
	public List<ProfessionalDetails> filterPastProDetails(List<ProfessionalDetails> prodetList){
		List<ProfessionalDetails> pastExpList = new ArrayList<ProfessionalDetails>(); 
		for(ProfessionalDetails pd:prodetList){
			if(!pd.getIsCurrent())
				pastExpList.add(pd);
		}
		return pastExpList;
	}
	
	public class CurrFrag extends Fragment{

		public CurrFrag(String id, MarkupContainer markupProvider, IModel<ProfessionalDetails> model) {
			super(id, "currFrag", markupProvider, model);
			add(new Label("currPos", new PropertyModel<String>(model, "positionTitle")));
			if(model.getObject().getProDetailType() == ProfessionalDetails.PRO_DETAIL_TYPE_DEGREE)
				add(new Label("join", "from"));
			else
				add(new Label("join", "at"));
			add(new Label("org", new PropertyModel<String>(model,"orgName")));
		}
		
	}

}
